package com.ytjj.qmyx.admin.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.ytjj.common.constants.TokenConstants;
import com.ytjj.common.service.RedisService;
import com.ytjj.qmyx.admin.dao.OrdersDao;
import com.ytjj.qmyx.admin.dao.UserDao;
import com.ytjj.qmyx.admin.model.request.UserAddRequest;
import com.ytjj.qmyx.admin.model.request.UserTrueRequest;
import com.ytjj.qmyx.admin.model.response.UsersResponse;
import com.ytjj.qmyx.admin.service.UserInfoService;
import com.ytjj.qmyx.mall.model.Orders;
import com.ytjj.qmyx.users.client.UsersClient;
import com.ytjj.qmyx.users.mapper.UsersInfoMapper;
import com.ytjj.qmyx.users.mapper.UsersMapper;
import com.ytjj.qmyx.users.model.TokenUsersRequest;
import com.ytjj.qmyx.users.model.Users;
import com.ytjj.qmyx.users.model.UsersInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Resource
    private UserDao userDao;
    @Resource
    private UsersMapper usersMapper;
    @Resource
    private OrdersDao ordersDao;
    @Resource
    private UsersClient usersClient;

    @Override
    public void updateUser(UserAddRequest request) {
        // 修改虚拟用户信息 Users
        int count = userDao.updateUser(request);
        if(count > 0) {
            // 修改虚拟用户信息 UsersInfo
            userDao.updateUserInfo(request);
        }
    }

    @Override
    public int updateUserScore(UserAddRequest request) {
        if (null == request.getId()) {
            return 0;
        }
//        int count = userDao.updateUserScore(request);
//        if (count <= 0) {
//            return 0;
//        }
        Users users = usersMapper.selectByPrimaryKey(request.getId());
        if ( users == null) {
            return 0;
        }
        ArrayList<String> telList = Lists.newArrayList(users.getTel());
        List<Orders> orders = ordersDao.queryTokenByTel(telList);
        if (!CollectionUtils.isEmpty(orders) && null != request.getScore()) {
            TokenUsersRequest tokenUsersRequest =new TokenUsersRequest();
            tokenUsersRequest.setToken(orders.get(0).getH5Token());
            tokenUsersRequest.setScore(request.getScore());
            usersClient.updateTokenUser(tokenUsersRequest);
        } else {
            TokenUsersRequest tokenUsersRequest =new TokenUsersRequest();
            tokenUsersRequest.setToken(users.getToken());
            tokenUsersRequest.setScore(request.getScore());
            usersClient.updateTokenUser(tokenUsersRequest);
        }

        return 1;
    }



    @Override
    public int updateTrueUser(UserTrueRequest request) {
        return userDao.updateTrueUser(request);
    }
}
